package com.acme.mock;


import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.exceptions.FeedLoaderException;

public class BankServer {
	ServerSocket providerSocket;
	Socket connection = null;
	
	public BankServer() {
	}

	public void run(Bank bank) throws FeedLoaderException {
		int i=0;
		
		try {
			providerSocket = new ServerSocket(2004, 10);
			System.out.println("Waiting for connection");
			
			while (true){
				connection = providerSocket.accept();
				System.out.println("Connection received from "
						+ connection.getInetAddress().getHostName());
			
				(new Thread( new ConnectionHandler(bank, connection) ) ).start();
				
				i++;
				System.out.println(i);
				
				try {
					Thread.sleep(20000);
					} catch(InterruptedException e) {
				} 
			}
			
		} catch (BindException bindExc) {
			System.out.println(bindExc.getMessage());
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}
}
